"""  
@author: lc  
@file: 13.剪刀石头布.py  
@time: 2024/8/23 14:32  
"""
#可以创建一个游戏对象
# 游戏对象可以加入到房间
# 同一个房间内的两个游戏对象可以进行游戏 一个房间最多两个人
# 每次两个游戏对象随机的出剪刀石头布
# 每次游戏三局两胜

#玩家
# 属性：姓名、房间
# 方法：战斗

#房间
#属性 房间号 房间成员
#方法：加入玩家。

import random
class Player :
    PlayerList = []
    def __init__(self,name,room=None):
        self.name = name
        self.room = room
        Player.PlayerList.append(self)
        if room!=None:
            for j in Room.RoomList:
                if room == j.room_id:
                    j.add_player(self)
                    print("玩家加入成功！")
    def fight(self,play):
        found_x = True
        num_self_count = 0
        num_play_count = 0
        while found_x:
            num_self = input("选择你的出招1剪刀，2石头，3布")
            num_play = random.randint(1, 3)
            if int(num_self)-num_play==-2 or int(num_self)-num_play==1:
                print(self.name,"胜了")
                num_self_count+=1
            if int(num_self)-num_play==-1 or int(num_self)-num_play ==2:
                print(play.name,"胜利")
                num_play_count+=1
            if int(num_self)-num_play==0:
                print("平局")
            if num_play_count==2:
                print(play.name,"赢了")
                found_x = False
            if num_self_count == 2:
                print(self.name,"赢了")
                found_x = False


class Room:
    RoomList = []
    def __init__(self,room_id):
        self.room_id =room_id
        self.room_members = []
        Room.RoomList.append(self)
    def add_player(self,player):
        if len(self.room_members)>=2:
            print("房间已经满了，请重新操作！")
        else:
            self.room_members.append(player)
            player.room = self
            print("加入房间成功")

#无限输入
#1、创建房间
#2、创建角色
#3、将角色加入房间
#4、选择房间战斗
#5、退出
while True:
    choice = input("输出你的选择：\n1、创建房间\n2、创建角色\n3、将角色加入房间\n4、战斗\n5、查看当前玩家和当前房间\n6、退出")
    if choice == "1":
        room_id = input("请输入房号：")
        r = Room(room_id)
        print("成功传讲房间",r.room_id)
    if choice =="2":
        name = input("请输入角色的名字：")
        r1 = input("可以输入房间号，或者不输入：")
        p = Player(name,r1)
        print()
    if choice == "3":
        player = input("请输入你要操作的对象")
        found_r = False
        found_p = False
        for i in Player.PlayerList:
            if player==i.name:
                room = input("请输入你要加入的房间号")
                found_p = True
                for j in Room.RoomList:
                    if room ==j.room_id:
                        j.add_player(i)
                        found_r = True
                        print("玩家加入成功！")
                if not found_r:
                    print("房间号不存在！")
        if not found_p:
            print("玩家不存在")
    if choice == "4":
        room = input("请输入你要战斗的房间号")
        found = False
        for j in Room.RoomList:
            if room == j.room_id:
                found = True
                print("选择房间成功！")
                if len(j.room_members)!=2:
                    print("人数不足，无法开始比赛！")
                else:
                    print(f"角色如下：0:{j.room_members[0].name},1:{j.room_members[1].name}")
                    choice1 = input("按顺序0或者1选择你要战斗的角色！另一方由电脑接管")
                    j.room_members[int(choice1)].fight(j.room_members[1-int(choice1)])
        if not found:
            print("房间号不存在！")
    if choice == "5":
        for ra in Room.RoomList:
            print(f"当前的房间：{ra.room_id}")
        for pa in Player.PlayerList:
            print(f"当前的玩家：{pa.name}")
    if choice == "6":
        break


